package com.qf.smsplatform.service.Impl;
/*2021/10/30*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.TAcountRecordCache;
import com.qf.smsplatform.constans.ResultCode;
import com.qf.smsplatform.events.TAcountRecordEvent;
import com.qf.smsplatform.exception.AddDataException;
import com.qf.smsplatform.exception.DeleteDataException;
import com.qf.smsplatform.mapper.TAcountRecordMapper;
import com.qf.smsplatform.pojo.CheckType;
import com.qf.smsplatform.pojo.MyAuthUser;
import com.qf.smsplatform.pojo.TAcountRecord;
import com.qf.smsplatform.pojo.TAdminUser;
import com.qf.smsplatform.service.TAcountRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Service
@Transactional
public class TAcountRecordServiceImpl implements TAcountRecordService {

    private TAcountRecordMapper acountRecordMapper;
    @Autowired
    public void setAcountRecordMapper(TAcountRecordMapper acountRecordMapper) {
        this.acountRecordMapper = acountRecordMapper;
    }

    private TAcountRecordCache tAcountRecordCache;
    @Autowired
    public void settAcountRecordCache(TAcountRecordCache tAcountRecordCache) {
        this.tAcountRecordCache = tAcountRecordCache;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public PageInfo<TAcountRecord> selectPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TAcountRecord> acountRecords = tAcountRecordCache.getAllData();
        List<TAcountRecord> collect = acountRecords.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        PageInfo<TAcountRecord> pageInfo=new PageInfo<>(collect);
        return pageInfo;
    }

    @Override
    public void insertTAcount(TAcountRecord tAcountRecord) {
        //判断添加信息是否为空
        if (tAcountRecord.isNull(CheckType.ADD)) {
            throw new AddDataException("添加的数据不合法", ResultCode.DATA_ISNULL);
        }
        //调用业务进行添加
        acountRecordMapper.insertTAcount(tAcountRecord);

        //声明一个事件
        context.publishEvent(new TAcountRecordEvent());

    }

    @Override
    public void updateTAcount(TAcountRecord tAcountRecord) {
        //判断修改信息是否为空
        if (tAcountRecord.isNull(CheckType.UPDATE)) {
            throw new AddDataException("修改的数据不合法", ResultCode.DATA_ISNULL);
        }
        acountRecordMapper.updateTAcount(tAcountRecord);

        //声明一个事件
        context.publishEvent(new TAcountRecordEvent());
    }

    @Override
    public void deleteTAcount(int id) {
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if(user.getType()==2){
            throw new DeleteDataException("当前用户没有权限进行数据的删除", ResultCode.NOT_ALLOW);
        }
        acountRecordMapper.deleteTAcount(id);

        //声明一个事件,更新缓存中的数据
        context.publishEvent(new TAcountRecordEvent());
    }
}
